<template>
  <div class="logo">
    <el-form class="box" :model="form" :rules="rules" ref="form">
      <img class="imgLogo" src="@/assets/common/login-logo.png" alt="" />
      <el-form-item prop="mobile">
        <el-input v-model="form.mobile">
          <template #prefix>
            <svg-icon iconClass="user"></svg-icon>
          </template>
        </el-input>
      </el-form-item>
      <el-form-item prop="password">
        <el-input v-model="form.password" type="password" show-password>
          <template #prefix>
            <svg-icon iconClass="password"></svg-icon>
          </template>
          <!-- <template #suffix>
             <svg-icon iconClass=''></svg-icon>
          </template> -->
        </el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" class="submit" @click="submit"
          >登录</el-button
        >
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  data () {
    return {
      form: {
        mobile: '13800000002',
        password: '123456'
      },
      rules: {
        mobile: [
          { required: true, message: '必填', trigger: 'change' },
          {
            validator: (rule, value, callback) => {
              const reg = /^(0|86|17951)?(13[0-9]|15[012356789]|166|17[3678]|18[0-9]|14[57])[0-9]{8}$/
              if (reg.test(value)) {
                callback()
              } else {
                callback(new Error('请输入11位正确的手机号'))
              }
            },
            message: '请输入11位正确的手机号',
            trigger: 'change'
          }
        ],
        password: [
          { required: true, message: '必填', trigger: 'change' },
          {
            min: 6,
            max: 12,
            message: '请输入6-12位密码',
            trigger: 'change'
          }
        ]
      }
    }
  },
  methods: {
    submit () {
      console.log(1)
      // 全局验证
      this.$refs.form.validate(async res => {
        if (res) {
          // 加async await 确保传值成功再调用接口
          await this.$store.dispatch('user/toLogin', this.form)
          this.$message.success('登录成功')
          //  跳转至首页
          const redirect = this.$route.query.redirect || '/'
          this.$router.push(redirect)
        } else {
          this.$message.error('登录失败')
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.logo {
  width: 100%;
  height: 100%;
  background: url(~@/assets/common/login.jpg);
  display: flex;
  justify-content: center;
  align-items: center;
  text-align: center;
  .box {
    width: 520px;
    height: 400px;
    padding: 0 35px;
    .imgLogo {
      padding-bottom: 40px;
    }
    .submit {
      width: 100%;
      padding: 20px;
      border: none;
      font-size: 25px;
    }
  }
}
::v-deep .el-input__inner {
  opacity: 0.8;
  height: 52px;
  padding-left: 35px;
  color: #68b0fe;
}
::v-deep .el-input__prefix {
  width: 30px;
  line-height: 50px;
}
</style>
